package cn.jwcat.controller;

import cn.jwcat.dealEntity.GoodsDto;
import cn.jwcat.entity.Claim;
import cn.jwcat.entity.Goods;
import cn.jwcat.entity.Type;
import cn.jwcat.service.IClaimService;
import cn.jwcat.service.IGoodsService;
import cn.jwcat.service.ITypeService;
import cn.jwcat.utils.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 物资操作
 */
@Transactional
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    IGoodsService goodsService;

    @Resource
    ITypeService typeService;

    @Resource
    IClaimService claimService;

    @GetMapping("/{page}/{size}")
    @ApiOperation("获取物资的全部信息")
    public Result getHealthInfoByStatus(@PathVariable("page") int page, @PathVariable("size") int size) {
        PageHelper.startPage(page, size);
        List<GoodsDto> goodsDtos = goodsService.getAllInfo();
        PageInfo<GoodsDto> info = new PageInfo<>(goodsDtos);
        return Result.success(info);
    }

    @PostMapping("/{page}/{size}")
    @ApiOperation("物资的模糊查询")
    public Result postQuery(@PathVariable("page") int page, @PathVariable("size") int size, @RequestBody Type type) {
        PageHelper.startPage(page, size);
        List<GoodsDto> goodsDtos = goodsService.postQuery(type);
        PageInfo<GoodsDto> info = new PageInfo<>(goodsDtos);
        return Result.success(info);
    }

    @PostMapping
    @ApiOperation("提交物资信息")
    public Result postHealth(HttpServletRequest request, @RequestBody Goods goods) {
        System.out.println(goods);
        boolean save = goodsService.save(goods);
        if (save) return Result.success("物资数据提交成功");
        return Result.error("物资数据提交失败");
    }

    @GetMapping("/option")
    @ApiOperation("获取物资类别信息")
    public Result getOption() {
        LambdaQueryWrapper<Type> typeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        List<Type> types = typeService.getBaseMapper().selectList(typeLambdaQueryWrapper);
        return Result.success(types);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id获取物资详细信息")
    public Result getById(@PathVariable String id) {
        Goods goods = goodsService.getBaseMapper().selectById(id);
        return Result.success(goods);
    }

    @PutMapping
    @ApiOperation("更新物资信息")
    public Result updateGoods(@RequestBody Goods goods) {
        int i = goodsService.getBaseMapper().updateById(goods);
        System.out.println(i);
        if (i == 1) return Result.success("物资更新成功");
        return Result.error("物资更新失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除物资信息")
    public Result deleteById(@PathVariable String id) {
        LambdaQueryWrapper<Claim> selectWrapper = new LambdaQueryWrapper<>();
        selectWrapper.eq(Claim::getGoodsId, id).eq(Claim::getStatus, "未处理");
        List<Claim> claims = claimService.getBaseMapper().selectList(selectWrapper);
        if (claims.size() != 0) return Result.error().message("删除失败，物资申领中还有患者需要该物资");
        LambdaQueryWrapper<Claim> deleteWrapper = new LambdaQueryWrapper<>();
        deleteWrapper.eq(Claim::getGoodsId, id);
        claimService.getBaseMapper().delete(deleteWrapper);
        int i = goodsService.getBaseMapper().deleteById(id);
        if (i == 1) return Result.success("物资删除成功");
        return Result.error("物资删除失败");
    }
}
